import Reflux from 'reflux'
import GUtils from '@/utils'

const prefix = '/system/user/super/'
const api = {
  getUserInfo: prefix + 'getUserInfo',
  getAllVipProductList: prefix + 'getAllVipProductList',
  mockRecharge: prefix + 'mockCreateRechargeOrder',
  mockVip: prefix + 'mockCreateVipOrder',
  getAllCouponList: '/coupon/option',
  issueCoupon: prefix + 'grant/coupon',
}

const createAction = () => {
  const Action = Reflux.createActions({
    unmount: {},
    getUserInfo: { asyncResult: true },
    getAllVipProductList: { asyncResult: true },
    mockRecharge: { asyncResult: true },
    mockVip: { asyncResult: true },
    getAllCouponList: { asyncResult: true },
    issueCoupon: { asyncResult: true },
  })

  Action.getUserInfo.listen(function (phone) {
    GUtils.Ajax.post(api.getUserInfo, { phone }).then(
      this.completed,
      this.failed
    )
  })
  Action.getAllVipProductList.listen(function () {
    GUtils.Ajax.post(api.getAllVipProductList, {}).then(
      this.completed,
      this.failed
    )
  })
  Action.mockRecharge.listen(function (vo) {
    GUtils.Ajax.post(api.mockRecharge, vo).then(this.completed, this.failed)
  })
  Action.mockVip.listen(function (vo) {
    GUtils.Ajax.post(api.mockVip, vo).then(this.completed, this.failed)
  })

  // 优惠券列表
  Action.getAllCouponList.listen(function () {
    GUtils.Ajax.get(api.getAllCouponList).then(this.completed, this.failed)
  })
  // 发券
  Action.issueCoupon.listen(function (vo) {
    GUtils.jsonAjax.post(api.issueCoupon, vo).then(this.completed, this.failed)
  })

  return Action
}

export { api, createAction }
